package com.medicalO2O.service.serviceimpl;

import com.medicalO2O.mapper.Attenance;
import com.medicalO2O.po.Schedule;
import com.medicalO2O.utils.DoctorServiceUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Kevin on 2015/12/30.
 */
public class UserServiceImpl {
    @Autowired
    private Attenance attenance;


    public Map<String, LinkedHashMap<String, Map<String, Map<String, Map<String, String>>>>> findDoctor(String day) {
        String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        Map<String, LinkedHashMap<String, Map<String, Map<String, Map<String, String>>>>> doctorMap = new HashMap();
        String[] dates = day.split("-"); //按照输入来的时间分割成年，月，日
        int y = Integer.parseInt(dates[0]);
        int m = Integer.parseInt(dates[1]) - 1;
        int d = Integer.parseInt(dates[2]);
        //查出一周的数据存在doctorMap中
        for (int i = 0; i < 7; i++) {
            cal.set(y, m, d + i);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (week_index < 0) {
                week_index = 0;
            }
            String week = weeks[week_index];
            String date = simpleDateFormat.format(cal.getTime());
            Schedule schedule1 = new Schedule();
            schedule1.setStart_time(date);
            LinkedList<Schedule> scheduleList = attenance.findDoctor(schedule1);
            for (Schedule s : scheduleList) {
                String doctorName = s.getDoctor().getFullname();
                String organizationName = s.getOrganization().getName();
                String departmentName = s.getDepartment().getName();
                String s_t = s.getStart_time();
                String e_t = s.getEnd_time();
                String startTime = s_t.substring(s_t.indexOf(" "), s_t.length() - 1);
                String endTime = e_t.substring(e_t.indexOf(" "), e_t.length() - 1);
                //初始化医生
                if (!doctorMap.containsKey(doctorName)) {
                    LinkedHashMap<String, Map<String, Map<String, Map<String, String>>>> dayMap = new LinkedHashMap();
                    doctorMap.put(doctorName, dayMap);
                }
                //初始化星期
                if (!doctorMap.get(doctorName).containsKey(week)) {
                    Map<String, Map<String, Map<String, String>>> organizationMap = new HashMap();
                    doctorMap.get(doctorName).put(week, organizationMap);
                }
                //初始化办公地点
                if (!doctorMap.get(doctorName).get(week).containsKey(organizationName)) {
                    Map<String, Map<String, String>> departmentMap = new HashMap();
                    doctorMap.get(doctorName).get(week).put(organizationName, departmentMap);
                }
                //初始化类别
                if (!doctorMap.get(doctorName).get(week).get(organizationName).containsKey(departmentName)) {
                    LinkedHashMap<String, String> dateMap = new LinkedHashMap();
                    doctorMap.get(doctorName).get(week).get(organizationName).put(departmentName, dateMap);
                }
                //将该医生的一天班表存在对应的map中
                doctorMap.get(doctorName).get(week).get(organizationName).get(departmentName).put(startTime, endTime);
            }


        }
        //处理doctorMap里面的数据
        DoctorServiceUtils.processMap(doctorMap);
        return doctorMap;
    }
}
